<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <DictTree class="w-1/4 xl:w-1/5" @select="handleSelect"/>
    <BasicTable class="w-3/4 xl:w-4/5" @register="registerTable"  :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleDictDataCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <DictModal @register="registerModal" @success="handleSuccess"/>
    <DictDataDrawer @register="registerDrawer" @success="reload"/>
  </PageWrapper>
</template>
<script lang="ts" setup name="dict">
import {reactive} from 'vue';
import {PageWrapper} from '@/components/page';
import {BasicTable, TableAction, ActionItem} from '@/components/table';
import {useDrawer} from '@/components/drawer';
import {useBaseTable} from '@/hooks';
import {useMessage} from '@/hooks/web/useMessage';
import {useModal} from "@/components/modal";

const {createMessage, createConfirm} = useMessage();
import DictTree from './components/DictTree.vue';
import DictModal from './components/DictModal.vue';
import DictDataDrawer from './components/DictDataDrawer.vue';
import {columns, searchFormSchema} from './data/data';
import {Api} from './data/dict.data.api';

const [registerModal, {openModal}] = useModal();
const [registerDrawer, {openDrawer}] = useDrawer();
//查询参数
const searchInfo = reactive<Recordable>({});

const {prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete} = useBaseTable({
  designScope: 'employee-template',
  tableProps: {
    title: '数据字典',
    rowKey: 'dictDataId',
    //分页列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    actionColumn: {
      width: 180,
      title: '操作',
      dataIndex: 'action',
      slots: {customRender: 'action'},
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, {reload}, {rowSelection, selectedRow, hasSelectedRows}] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    }
  ];
}


/**
 * 打开字典分类弹窗
 */
function handleCreate() {
  openModal(true, {
    isUpdate: false,
    dictId: searchInfo.dictId,
  });
}

/**
 * 打开字典项抽屉
 */
function handleDictDataCreate() {
  openDrawer(true, {
    isUpdate: false,
    dictId: searchInfo.dictId,
  });
}

/**
 * 编辑字典项
 * @param record
 */
function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 字典分类选择事件
 * @param dictId
 */
function handleSelect(dictId) {
  searchInfo.dictId = dictId;
  reload();
}
</script>
